package com.starbaba.stat.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.StatUserCoreModel;
import com.starbaba.stat.utils.Constant;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.service.LoanBaseService;

@SuppressWarnings("rawtypes")
@Component
public class StatUserCoreService {

	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);
	
	public void showPage(Map<String, Object> paramsMap) throws Exception {
		List<CommProductPrdid> prdList = LoanBaseService.getProductList();
		List<CommProductChannel> channelList = LoanBaseService.getChannelList();
		CommProductPrdid allProd = new CommProductPrdid();
		allProd.setProdCode(-1000);
		allProd.setName("全部");
		prdList.add(0, allProd);
		CommProductChannel allChannel = new CommProductChannel();
		allChannel.setChannel_name("全部");
		allChannel.setChannel_code(-1000);
		channelList.add(0, allChannel);
		paramsMap.put("prdList", prdList);
		paramsMap.put("channelList", channelList);
	}
	
	public String getExportString(PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception{
		List<StatUserCoreModel> list = getList(null,null,pageResponse,paramsMap);
		StringBuilder builder = new StringBuilder();
		builder.append("日期,平台,产品,渠道,激活周期,新增人数,新增登陆人数,注册成功人数,签到成功人数,大转盘抽奖人数,每周大奖人数,创建资料人数,"
				+ "API进件数,贷款申请成功数").append("\r\n");
		for(StatUserCoreModel core : list){
			builder.append(core.getDate()).append(","); 
			builder.append(core.getPlatform()).append(","); 
			builder.append(core.getPrd_name()).append(","); 
			builder.append(core.getChannel_name()).append(","); 
			builder.append(core.getMode()).append(",");
			builder.append(core.getIncrease()).append(",");
			builder.append(core.getNew_login()).append(",");
			builder.append(core.getRegister()).append(",");
			builder.append(core.getSign()).append(",");
			builder.append(core.getLucky_turn()).append(",");
			builder.append(core.getLucky_week()).append(",");
			builder.append(core.getSetup()).append(",");
			builder.append(core.getApi_in()).append(",");
			builder.append(core.getLoan()).append(",");
			builder.append("\r\n");
		}
		return builder.toString();
	}
	
	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {

		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String platform = (String) paramsMap.get("platform");
		String prd_id = (String) paramsMap.get("prd_id");
		String channel = (String) paramsMap.get("channel"); 
		String mode = (String) paramsMap.get("mode"); 
		String type = (String) paramsMap.get("type"); 
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startTime)) {
			whereParams.append("and date >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)) {
			whereParams.append("and date <= '" + endTime + "' ");
		}
		if (StringUtils.isNotEmpty(platform) && !"all".equals(platform)) {
			whereParams.append("and a.platform = '" + platform + "' ");
		}
		if (StringUtils.isNotEmpty(prd_id) && !"-1000".equals(prd_id)) {
			whereParams.append("and prd_id = '" + prd_id + "' ");
		}
		if (StringUtils.isNotEmpty(channel) && !"-1000".equals(channel)) {
			whereParams.append("and channel = '" + channel + "' ");
		}
		if (StringUtils.isNotEmpty(mode)) {
			whereParams.append("and `mode` = '" + mode + "' ");
		}
		
		StringBuilder groupBy = new StringBuilder();
		groupBy.append("group by date, `mode`, ");
		if (!"all".equals(platform)) {
			groupBy.append("a.platform, ");
		}
		if (!"-1000".equals(prd_id)) {
			groupBy.append("prd_id, ");
		}
		if (!"-1000".equals(channel)) {
			groupBy.append("channel, ");
		}
		String groupByString = groupBy.toString().substring(0, groupBy.length() - 2) + " ";
		
		StringBuilder listSql = new StringBuilder("SELECT date, a.platform, b.name prd_name, c.channel_name, `mode`, sum(increase) increase,sum(new_login) new_login, sum(register) register, sum(sign) sign, ");
		listSql.append("sum(lucky_turn) lucky_turn, sum(lucky_week) lucky_week, sum(setup) setup, sum(api_in) api_in, sum(loan) loan from stat.stat_user_core a, ");
		listSql.append("loan.comm_product_prdid b, loan.comm_product_channel c where a.prd_id = b.prdid and a.channel = c.channel_code ");
		listSql.append("");
		listSql.append("");
		
		listSql.append(whereParams).append(groupByString);
		listSql.append("order by date desc ");
		
		String sqlCount = "select count(1) from (" + listSql.toString() + ") z ";
		if (start != null && length != null) {
			listSql.append("limit " + start + ", " + length);
		}
		int count = dao.getInt(sqlCount);
		List<StatUserCoreModel> list = dao.list(listSql.toString(), StatUserCoreModel.class);
		for (StatUserCoreModel model : list) {
			if ("all".equals(platform)) {
				model.setPlatform("全部");
			}
			if ("-1000".equals(prd_id)) {
				model.setPrd_name("全部");
			}
			if ("-1000".equals(channel)) {
				model.setChannel_name("全部");
			}
			if ("1".equals(type)) {
				// 百分比显示
				model.setNew_login(getPercent(model.getNew_login(), model.getIncrease()));
				model.setRegister(getPercent(model.getRegister(), model.getIncrease()));
				model.setSign(getPercent(model.getSign(), model.getIncrease()));
				model.setLucky_turn(getPercent(model.getLucky_turn(), model.getIncrease()));
				model.setLucky_week(getPercent(model.getLucky_week(), model.getIncrease()));
				model.setSetup(getPercent(model.getSetup(), model.getIncrease()));
				model.setApi_in(getPercent(model.getApi_in(), model.getIncrease()));
				model.setLoan(getPercent(model.getLoan(), model.getIncrease()));
			}
		}
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		return list;
	}
	
	public static String getPercent(Long a, Long b) {
		return (float) Math.round((float) a / b * 10000) / 100 + "%";
	}
	
	public static String getPercent(String a, String b) {
		if (StringUtils.isEmpty(a) || StringUtils.isEmpty(b)){
			return "0";
		}
		Long al = Long.parseLong(a);
		Long bl = Long.parseLong(b);

		return (float) Math.round((float) al / bl * 10000) / 100 + "%";
	}
	
}
